package com.dodol.builder;

import com.dodol.common.*;
import com.dodol.util.ElementFinder;
import org.openqa.selenium.WebDriver;

/**
 * @Auther: 80736
 * @Date: 2018/9/1 22:20
 * @Description:
 */
public class TestStepFactory {

    private static TestStep step;

    private String actionType;  //步骤类型
    private String elementSearchType;   //元素查找方式
    private String elementSearchValue;  //元素查找参数
    private String enterValue;  // 输入数值
    private String verityKeyword;   //验证关键字
    private String vertiyElementSearchType; //验证元素查找方式
    private String vertiyElementSearchValue;    //验证元素查找参数


    /**
     * 构建一条测试步骤
     *
     * @return
     */
    public TestStep buildStep(WebDriver driver) {
        ExpectedVerification verification = null;
        //存在校验步骤则生成校验对象
        if (verityKeyword != null && !verityKeyword.equals("")) {
            verification = new ExpectedVerification();
            verification.setVerityType(Vertiy.valueOf(verityKeyword));
            verification.setVerityValue(vertiyElementSearchValue);
            verification.setDriver(driver);

            // 在校验时如果不需要查找元素，则不需要设置元素对象到校验类
            if (vertiyElementSearchType != null && !vertiyElementSearchType.equals("")) {
                ElementFinder.Element verityElement = new ElementFinder.Element(FindElementBy.valueOf(vertiyElementSearchType), vertiyElementSearchValue);
                verification.setElement(verityElement);
            }
        }


        //构建测试步骤
        TestStep step = new TestStep();
        step.setUserActionType(UserAction.valueOf(actionType));
        //操作步骤里，如果不需查找元素，则不需要设置元素对象到step
        if (elementSearchType != null && !elementSearchType.equals("")) {
            step.setElement(new ElementFinder.Element(FindElementBy.valueOf(elementSearchType), elementSearchValue));
        }

        step.setEnterValue(enterValue);

        // 如果不需要校验，则不设置校验对象
        if (verification != null) {
            step.setVerification(verification);
        }
        return step;

    }

    public static TestStep getStep() {
        return step;
    }

    public static void setStep(TestStep step) {
        TestStepFactory.step = step;
    }

    public String getActionType() {
        return actionType;
    }

    public void setActionType(String actionType) {
        this.actionType = actionType.toUpperCase();
    }

    public String getElementSearchType() {
        return elementSearchType;
    }

    public void setElementSearchType(String elementSearchType) {
        if (elementSearchType != null) {
            this.elementSearchType = elementSearchType.toUpperCase();
        }
    }

    public String getElementSearchValue() {
        return elementSearchValue;
    }

    public void setElementSearchValue(String elementSearchValue) {
        this.elementSearchValue = elementSearchValue;
    }

    public String getEnterValue() {
        return enterValue;
    }

    public void setEnterValue(String enterValue) {
        this.enterValue = enterValue;
    }

    public String getVerityKeyword() {
        return verityKeyword;
    }

    public void setVerityKeyword(String verityKeyword) {
        if (verityKeyword != null) {
            this.verityKeyword = verityKeyword.toUpperCase();
        }

    }

    public String getVertiyElementSearchType() {
        return vertiyElementSearchType;
    }

    public void setVertiyElementSearchType(String vertiyElementSearchType) {
        if (vertiyElementSearchType != null) {
            this.vertiyElementSearchType = vertiyElementSearchType.toUpperCase();
        }

    }

    public String getVertiyElementSearchValue() {
        return vertiyElementSearchValue;
    }

    public void setVertiyElementSearchValue(String vertiyElementSearchValue) {
        this.vertiyElementSearchValue = vertiyElementSearchValue;
    }
}
